package com.google.code.dataimport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;

import com.google.code.soumi.dao.Users;
import com.google.code.soumi.service.SoumiService;
import com.ibatis.sqlmap.client.SqlMapClient;

public class RunSoumiDataImport {
	
	private static SqlMapClient sqlMapClient = SoumiService.getInstance().getSqlMapClient();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//导入CSDN用户数据
		importCsdnUsers(0, "C:/Documents and Settings/Administrator/桌面/数据库/www.csdn.net.sql");


		//导入天涯用户数据
		importTianyaUsers(0, "C:/Documents and Settings/Administrator/桌面/数据库/天涯数据");

	}
	
	
	/**
	 * 导入天涯用户数据
	 * @param start 开始记录
	 * @param dir 数据目录
	 */
	public static void importTianyaUsers(long start, String dir){
		try{
			File directory = new File(dir);
			File[] files = directory.listFiles();
			
			if(files==null || files.length<=0)
				return;
			
			long i = 0;
			int batch = 0;
			
			// 遍历数据文件
			for(File file : files){
				BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));  
				
				String line;
				while ((line = br.readLine()) != null) {
					if(i++<start)
						continue;
					
					if(line==null || line.trim().equals(""))
						continue;
					
					String[] arr = line.split("\\s+");
					
					if(arr==null || arr.length<=0 || arr[0]==null)
						continue;
					
					synchronized (line) {
						if(batch==0){
							sqlMapClient.startTransaction();
							sqlMapClient.startBatch();
						}
						
						Users user = new Users();
						try{
							user.setUsername(arr[0].trim());
							user.setPassword(arr[1]==null?"":arr[1].trim());
							user.setEmail(arr[2]==null?"":arr[2].trim());
						}catch(ArrayIndexOutOfBoundsException e){}
						
						sqlMapClient.insert("addTianyaUsers", user);
						batch++;
						
						System.out.println("天涯" + " -- " + i + " : " + line);
						
						if(batch==1000){
							sqlMapClient.executeBatch();
							sqlMapClient.commitTransaction();
							sqlMapClient.endTransaction();
							batch = 0;	
							line.wait(100);
						}
					}
					
				}
				 
				br.close();
			}
		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
			
	}
	
	
	/**
	 * 导入CSDN用户数据
	 * @param start 开始记录
	 * @param file 数据文件
	 */
	public static void importCsdnUsers(long start, String file){
		try{
			BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));  
			
			String line;
			long i = 0;
			int batch = 0;
			
			while ((line = br.readLine()) != null) {
				if(i++<start)
					continue;
				
				if(line==null || line.trim().equals(""))
					continue;
				
				String[] arr = line.split("#");
				
				if(arr==null || arr.length<=0 || arr[0]==null)
					continue;
				
				synchronized (line) {
					if(batch==0){
						sqlMapClient.startTransaction();
						sqlMapClient.startBatch();
					}
					
					Users user = new Users();				
					try{
						user.setUsername(arr[0]==null?"":arr[0].trim());
						user.setPassword(arr[1]==null?"":arr[1].trim());
						user.setEmail(arr[2]==null?"":arr[2].trim());
					}catch(ArrayIndexOutOfBoundsException e){}
					
					sqlMapClient.insert("addCsdnUsers", user);
					batch++;
	
					System.out.println("CSDN" + " -- " + i + " : " + line);
					
					if(batch==1000){
						sqlMapClient.executeBatch();
						sqlMapClient.commitTransaction();
						sqlMapClient.endTransaction();
						batch = 0;
						line.wait(100);
					}
				}
				
			}
			 
			br.close();
		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
			
	}
	
	

}
